n=871;X=Table[i,{i,Ceiling[Sqrt[n]],((n+1)/2)-1}];

X

X3=Select[X,Mod[#,3]==1|| Mod[#,3]==2&](* a=1,2 (mod 3),  as n=1 (mod 3)  *)

X5=Select[X3,Mod[#,5]==0|| Mod[#,5]==1||Mod[#,5]==4&](* a=0,1,4 (mod 5), as n=1 (mod 5)*)

Y=X7=
Select[X5,Mod[#,7]==0|| Mod[#,7]==2||Mod[#,7]==5&](* a=0,2,5 (mod 7), as n=3 (mod 7)*)

(** Now use Fermat factorization **)

IsSquare[n_]:=IntegerQ[Sqrt[n]] (* whether n is square *)

For[i=1,i<=Length[Y],i++,
If[IsSquare[((Y[[i]])^2)-n], Print[Y[[i]]+Sqrt[((Y[[i]])^2)-n],
"\[Cross]",
Y[[i]]-Sqrt[((Y[[i]])^2)-n], "=",n];Break[],
Print[Y[[i]],"^2-",n," is not square"]]
]